package com.zzy.factory.simplefactory.pizzastore.order;

import com.zzy.factory.simplefactory.pizzastore.pizza.Pizza;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * 订单类
 * 组合了一个简单工厂
 */
public class Order {
    public static void main(String[] args) {
        new Order(new SimplePizzaFactory());
    }

    SimplePizzaFactory simpleFactory;
    Pizza pizza = null;

    public Order(SimplePizzaFactory simpleFactory) {
        setSimpleFactory(simpleFactory);
    }

    public void setSimpleFactory(SimplePizzaFactory simpleFactory) {
        this.simpleFactory = simpleFactory;

        //用户输入
        String orderType;

        while (true) {
            orderType = getType();
            pizza = this.simpleFactory.createPizza(orderType);
            //简单工厂 静态
            //pizza = this.simpleFactory.createPizza1(orderType);
            if (pizza != null) {
                pizza.prepare();
                pizza.bake();
                pizza.cut();
                pizza.box();
            } else {
                System.out.println("pizza为空");
                break;
            }
        }


    }

    /**
     * 读取输入
     *
     * @return orderType
     */
    private String getType() {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("请输入Pizza种类：");
        String line = null;
        try {
            line = bufferedReader.readLine();
        } catch (IOException e) {
            e.printStackTrace();
            return "";
        }
        return line;
    }
}
